/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package sqlserver;

/**
 *
 * @author BEHIEN
 */
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

public class MetaFile {
	public static final int ErrorUnknown=-1;
	public static final int ErrorLoading=-2;
	public static final int ErrorExisted=-3;
	public static final int ErrorInvalidAgrument=-4;
	public static final int ErrorDBNotFound=-5;
	public static final int ErrorTableNotFound=-6;
	public static final int ErrorColumnNotFound=-7;
	public static final int ErrorConstraintNotFound=-8;
	public static final int ErrorConstraintReferenceNotFound=-9;
	public static final int ErrorInvalidTypeOnColumnReference=-10;
	public static final int Success=1;
	private static final String META_Path = "Database.xml";

	public static Document LoadDoc() {
		File file = new File("DataBase.xml");
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db;
		Document doc = null;
		try {
			db = dbf.newDocumentBuilder();
			doc = db.parse(file);
		} catch (ParserConfigurationException | SAXException e) {
		} catch (IOException e) {
			// error file meta not found. Create new file meta
			CreateDoc();
			try {
				db = dbf.newDocumentBuilder();
				doc = db.parse(file);
			} catch (    ParserConfigurationException | SAXException | IOException e1) {
			}
		}
		return doc;
	}

	private static void CreateDoc() {
		DocumentBuilderFactory docFactory = DocumentBuilderFactory
				.newInstance();
		DocumentBuilder docBuilder;
		try {
			docBuilder = docFactory.newDocumentBuilder();
			Document doc = docBuilder.newDocument();
			Element rootElement = doc.createElement("Databases");
			doc.appendChild(rootElement);
			Save(doc);
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
		}

	}

	public static int Save(Document doc) {
		File file = new File("Database.xml");
		TransformerFactory transformerFactory = TransformerFactory
				.newInstance();
		Transformer transformer = null;
		try {
			transformer = transformerFactory.newTransformer();
			DOMSource source = new DOMSource(doc);
			StreamResult result = new StreamResult(file);
			try {
				transformer.transform(source, result);
				return Success;
			} catch (TransformerException e) {
				return ErrorUnknown;
			}
		} catch (TransformerConfigurationException e) {
			return ErrorUnknown;
		}

	}
}